package com.lisence.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.calmcar.adas.apiserver.AdasConf;

/**
 * Created by Carson_Ho on 16/10/31.
 */
public class NetWorkStateReceiver extends BroadcastReceiver {
    public NetWorkStateReceiver(LisenceManager lisenceManager) {
        this.lisenceManager = lisenceManager;
    }
    private LisenceManager lisenceManager ;
    @Override
    public void onReceive(Context context, Intent intent) {
//        System.out.println("网络状态发生变化");
        //检测API是不是小于23，因为到了API23之后getNetworkInfo(int networkType)方法被弃用
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiNetworkInfo.isConnected() && wifiNetworkInfo.isAvailable()) {
                  if(!AdasConf.IS_ACTIVE){
                      lisenceManager.checkLicenceDate();
                  }
//                Toast.makeText(context, "WIFI已连接,且可用", Toast.LENGTH_SHORT).show();
            } else if (dataNetworkInfo.isConnected() && dataNetworkInfo.isAvailable()) {
                    if(!AdasConf.IS_ACTIVE){
                    lisenceManager.checkLicenceDate();
                    }
//                Toast.makeText(context, "数据已连接,且可用", Toast.LENGTH_SHORT).show();
            } else  {
//                Toast.makeText(context, "网络连接不可用", Toast.LENGTH_SHORT).show();
            }
        }else {//API大于23时使用下面的方式进行网络监听
//            System.out.println("API level 大于23");
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取所有网络连接的信息
            Network[] networks = connMgr.getAllNetworks();
            //用于存放网络连接信息
            StringBuilder sb = new StringBuilder();
            //Mobile-wifi
            NetworkInfo mobileInfo=null,wifiInfo=null;
            //通过循环将网络信息逐个取出来
            for (int i=0; i < networks.length; i++){
                //获取ConnectivityManager对象对应的NetworkInfo对象
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                if(networkInfo==null ) continue;
                String typeName=networkInfo.getTypeName();
                if("MOBILE".equals(typeName)){
                  mobileInfo=networkInfo;
                }else if("WIFI".equals(typeName)){
                    wifiInfo =networkInfo;
                }
            }
             if(mobileInfo!=null){
                 if(mobileInfo.isConnected() && mobileInfo.isAvailable()){
                     if(!AdasConf.IS_ACTIVE) {
                         lisenceManager.checkLicenceDate();
                     }
                 }
             }else if(wifiInfo != null){
                 if(wifiInfo.isConnected() && wifiInfo.isAvailable()){
                     if(!AdasConf.IS_ACTIVE){
                         lisenceManager.checkLicenceDate();
                     }
                 }
             }
//            Toast.makeText(context, sb.toString(),Toast.LENGTH_SHORT).show();
        }
    }
}
